package com.atguigu.juc.test01;

import java.util.concurrent.locks.Condition;
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;

public class scTest {
    //线程  操作  资源类
    public static void main(String[] args) {
        caiguan cai = new caiguan();

        for (int i = 0; i < 30; i++) {
            new Thread(()->{
                cai.Zuocai();
            },"i"+i).start();
        }
        for (int i = 0; i < 30; i++) {
            new Thread(()->{
                cai.ShangCai();
            },"b"+i).start();
        }


       /* new Thread(() -> {
            for (int i = 0; i < 100; i++) {
                cai.ShangCai();
            }

        }, "a").start();
        new Thread(() -> {
            for (int i = 0; i < 100; i++) {
                cai.Zuocai();
            }

        }, "b").start();
        new Thread(() -> {
            for (int i = 0; i < 100; i++) {
                cai.Zuocai();
            }

        }, "c").start();
        new Thread(() -> {
            for (int i = 0; i < 100; i++) {
                cai.ShangCai();
            }

        }, "d").start();
*/
    }
    // 判断 操作 通知
}

class caiguan {   //资源类


    private int i = 0;
    private Lock lock = new ReentrantLock(true);
    private Condition concition = lock.newCondition();

    public void ShangCai() {

        lock.lock();  // block until condition holds
        try {//判断
            while (i == 0) {
                concition.await();
            }//干活
            i--;
            System.out.println(Thread.currentThread().getName() + "  :" + i);
            //通知
            concition.signalAll();
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            lock.unlock();
        }


    }

    public synchronized void Zuocai() {
        lock.lock();  // block until condition holds
        try {
            while (i != 0) {
                try {
                    concition.await();
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
            i++;
            System.out.println(Thread.currentThread().getName() + "  :" + i);
            concition.signalAll();
        } finally {
            lock.unlock();
        }


    }


    /**
     *
     * synchronized 多线程  上菜
     * */


    /*  private  int i=0;
      public synchronized void ShangCai(){
          while (i==0) {
              try {
                  this.wait();
              } catch (InterruptedException e) {
                  e.printStackTrace();
              }
          }
          i--;
          System.out.println(Thread.currentThread().getName()+"  :"+i );
          this.notifyAll();
      }
      public synchronized void Zuocai(){
          while (i!=0) {
              try {
                  this.wait();
              } catch (InterruptedException e) {
                  e.printStackTrace();
              }
          }
          try {
              Thread.sleep(40);
          } catch (InterruptedException e) {
              e.printStackTrace();
          }
          i++;
          System.out.println(Thread.currentThread().getName()+"  :" + i);
          this.notifyAll();
      }
*/

    //资源类synchronized案例 要求判断要用while 进行多线程判断
   /*
    private  int i=0;
    public synchronized void ShangCai(){
        if (i==0) {
            try {
                this.wait();
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
        i--;
        System.out.println(Thread.currentThread().getName()+"  :"+i );
        this.notifyAll();
    }
    public synchronized void Zuocai(){
        if (i!=0) {
            try {
                this.wait();
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
        try {
            Thread.sleep(40);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        i++;
        System.out.println(Thread.currentThread().getName()+"  :" + i);
        this.notifyAll();
    }
*/

}
